home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / bin / geomutil / math2oogl / math2oogl.c < prev    next >
C/C++ Source or Header  |  1993-12-07  |  21KB  |  834 lines

  1. /* Copyright (c) 1993 The Geometry Center; University of Minnesota
  2.    1300 South Second Street Suite 500;  Minneapolis, MN  55454, USA;
  3.    
  4. This is file math2oogl.c.  math2oogl.c is free software.  You can redistribute
  5. it and/or modify it only under the terms given in the file COPYING,
  6. which you should have received along with this file.  this and other
  7. related software may be obtained via anonymous ftp from geom.umn.edu;
  8. email: software@geom.umn.edu. */
  9.  
  10. /* math2oogl: convert Mathematica graphics object to OOGL format.
  11.    SurfaceGraphics and MeshGraphics => MESH, Graphics3D => OFF,
  12.    BezierPatch => BEZuvn.
  13.  
  14.    Note that we expect the graphics objects to have been processed as
  15.    in OOGL.m, i.e. provide dimension and meshrange information and
  16.    print out the colors before the points for SurfaceGraphics objects,
  17.    and convert the characters "(){}, " to a newline.
  18.  
  19.    Original converter by Nils McCarthy, 
  20.    Geomview pipefitting by Stuart Levy
  21.    SurfaceGraphics converter additions by Tamara Munzner.
  22.    MeshGraphics and BezierPatch converter additions by Silvio Levy
  23. */
  24.  
  25.  
  26.  
  27. /*
  28.  * Pipe fitting for linking Mathematica to geomview.
  29.  * Starts geomview if not already running.
  30.  */
  31.  
  32.  
  33. #include <stdio.h>
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include <sys/signal.h>
  37. #include <sys/file.h>
  38. #include <sys/fcntl.h>
  39. #include <sys/errno.h>
  40. #include <sys/socket.h>
  41. #include <sys/un.h>
  42. #include <string.h>
  43. #include <stdlib.h>
  44.  
  45. #ifndef FNONBLK        /* Next 3.0 lacks these in <sys/fcntl.h> */
  46. #define FNONBLK 0
  47. #endif
  48.  
  49. #ifndef FD_CLOEXEC
  50. #define FD_CLOEXEC 1
  51. #endif
  52.  
  53. extern int errno;
  54.  
  55. char *todir = "/tmp/geomview";
  56. char *toname = "Mathematica";
  57.  
  58. char giveup[] = "Geomview graphics: math2oogl: Couldn't start geomview on ";
  59.  
  60. static void interrupt(int sig) { 
  61.     char myname[1024];
  62.     write(2, giveup, sizeof(giveup));
  63.     gethostname(myname, sizeof(myname));
  64.     write(2, myname, strlen(myname));
  65.     write(2, "\n", 1);
  66.     exit(1);
  67. }
  68.  
  69. start_gv(char **gvpath, char *pipename, char *toname)
  70. {
  71.     char *args[1024];
  72.     int i = 0;
  73.     char **gvp;
  74.  
  75.     signal(SIGALRM, interrupt);
  76.     args[i++] = gvpath[0];
  77.  
  78. #ifdef NeXT
  79.     args[i++] = "-Mc";  args[i++] = toname;
  80. #else /* sgi */
  81.     args[i++] = "-c";   args[i++] = pipename;
  82. #endif
  83.     /* Copy remaining args through trailing NULL */
  84.     for(gvp = gvpath; (args[i++] = *++gvp); )
  85.     ;
  86.  
  87.     if(fork() == 0) {
  88.     int savederr = dup(2);
  89.     char myname[1024];
  90.     static char whynot[] = "Geomview graphics: math2oogl: Couldn't find ";
  91.  
  92.     fcntl(savederr, F_SETFD, FD_CLOEXEC);    /* close this on exec */
  93.     close(0); close(1); close(2);
  94.  
  95.  
  96.     open("/dev/null", O_RDWR);    /* Open /dev/null as file descriptors */
  97.     dup(0);  dup(0);        /* 0(stdin), 1(stdout) and 2(stderr) */
  98.         /* (Could just close them, but that seems to poison geomview
  99.          * if it tries to report an error.)
  100.          */
  101.  
  102.     setpgrp(0,getpid());
  103.     execvp(gvpath[0], args);
  104.     write(savederr, whynot, sizeof(whynot));
  105.     write(savederr, gvpath[0], strlen(gvpath[0]));
  106.     write(savederr, "\n", 1);
  107.     execvp("geomview", args);
  108.     execvp("gv", args);
  109.  
  110.     dup2(savederr, 2);
  111.     kill(getppid(), SIGALRM);
  112.     interrupt(0);
  113.     _exit(1);
  114.     }
  115. }
  116.  
  117. startgv(char **gvpath)
  118. {
  119.     int usesock;
  120.     int n, fd = -1;
  121.     char pipename[BUFSIZ];
  122.     struct sockaddr_un un;
  123.  
  124.     if(access(todir, W_OK) < 0) {
  125.     mkdir(todir, 0777);
  126.     chmod(todir, 0777);
  127.     }
  128.     sprintf(pipename, "%s/%s", todir, toname);
  129.  
  130. #ifdef NeXT
  131.     usesock = 1;
  132. #else
  133.     usesock = 0;
  134. #endif
  135.  
  136.     if(usesock) {
  137.     strncpy(un.sun_path, pipename, sizeof(un.sun_path)-1);
  138.     un.sun_family = AF_UNIX;
  139.     fd = socket(AF_UNIX, SOCK_STREAM, 0);
  140.     if(connect(fd, (struct sockaddr *)(&un), sizeof(un)) < 0) {
  141.         if(errno != ECONNREFUSED && errno != ENOENT) {
  142.         fprintf(stderr, "togeomview: Can't connect to ");
  143.         perror(pipename);
  144.         exit(1);
  145.         }
  146.  
  147.         start_gv(gvpath, pipename, toname);
  148.         for(n = 0; connect(fd, (struct sockaddr *)(&un), sizeof(un)) < 0; n++) {
  149.         if(n == 60)
  150.             interrupt(0);
  151.         sleep(1);
  152.         }
  153.     }
  154.     } else {
  155.     /* Use named pipe */
  156. #ifndef NeXT
  157.     if(access(pipename, 0) < 0) {
  158.         mknod(pipename, S_IFIFO, 0);
  159.         chmod(pipename, 0666);
  160.     }
  161.     fd = open(pipename, O_WRONLY|O_NONBLOCK);
  162.     if(fd >= 0) {
  163.         fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~(FNDELAY|FNONBLK));
  164.     } else if(errno == ENXIO) {
  165.         start_gv(gvpath, pipename, toname);
  166.         alarm(60);
  167.         fd = open(pipename, O_WRONLY);
  168.         alarm(0);
  169.     }
  170. #endif
  171.     }
  172.     if(fd < 0) {
  173.     fprintf(stderr, "Can't open pipe to geomview: ");
  174.     perror(pipename);
  175.     exit(1);
  176.     }
  177.  
  178.     /* Now stdout writes to pipe. */
  179.     dup2(fd, 1);
  180. }
  181.  
  182.  
  183. /*
  184.  * Mathematica->OOGL conversion
  185.  */
  186.  
  187. typedef float Color[3];
  188.  
  189. #define ismajor isupper
  190.  
  191. /*
  192.  * The following table should always obey the convention that "major"
  193.  * tokens (corresponding to the cases in the big switch below)
  194.  *  are represented by capitals
  195.  */
  196.  
  197. enum st {
  198.   IGNORE='i',
  199.   POLYGON='p',
  200.   LINE='l',
  201.   COLOR='c',
  202.   NUMBER='n',
  203.   MESH='m',
  204.   MESHRANGE='k',
  205.   DIMENSIONS='d',
  206.   SG='S',
  207.   MG='M',
  208.   G3='G',
  209.   BG='B'
  210. };
  211.  
  212. struct line {
  213.   char *data;
  214.   enum st token;
  215.   struct line *next;
  216. };
  217.  
  218. struct line *lines=NULL;
  219. int size[2];
  220. float range[4];
  221.  
  222. void usage()
  223. {
  224.   fprintf(stderr,"Usage:  math2oogl [-togeomview <objectname>]");
  225.   exit(1);
  226. }
  227.  
  228. int main(int ac,char **av)
  229. {
  230.   int togv = 0;
  231.   struct line *lastline=NULL;
  232.   struct line *prev=NULL;
  233.   char buf[1024];
  234.   int npolypoints=0, npolys=0, npolycolors=0;
  235.   int nvectpoints=0, nvects=0, nvectcolors=0;
  236.   struct line *curline, *globline;
  237.   int numnums=0;
  238.   int numcols=0;
  239.   enum st state=IGNORE;
  240.   Color **colors;
  241.   int i,j,q,ok;
  242.   float xincr, yincr;
  243.   int complex = 0, toss = 0;
  244.  
  245.   if (ac >= 4 && (!strcmp(av[1],"-togeomview"))) {
  246.     startgv(&av[3]);
  247.     printf("(geometry %s\n", av[2]);
  248.     togv=1;
  249.   } else if (ac > 1) usage();
  250.  
  251.   for(;;) {
  252.     int c;
  253.     char *k;
  254.     /* Swallow all delimiters, " (){},\n\r".
  255.      * It's not strictly correct to ignore {} nesting, but...
  256.      */
  257.     for(k = buf; (c = getchar()) > ' ' && c != ',' && c != '(' && c != ')'
  258.                 && c != '{' && c != '}' && k < buf+sizeof(buf)-1;
  259.             k++) {
  260.     /* Handle escaped newlines, which Mathematica inserts just rarely
  261.      * enough to be hard to notice!
  262.      */
  263.     if(c == '\\') {
  264.         if((c = getchar()) == '\n') {
  265.         while((c = getchar()) == ' ' || c == '\t')
  266.             ;
  267.         }
  268.     }
  269.     *k = c;
  270.     }
  271.     if(c == EOF)
  272.     break;
  273.     *k = '\0';
  274.     if (k == buf || !strcmp(buf,"List"))
  275.       continue;
  276.  
  277.     if (!lastline)
  278.       lastline = lines = malloc(sizeof(struct line));
  279.     else {
  280.       if (toss) lastline = prev;
  281.       toss = 0;
  282.       prev = lastline;
  283.       lastline = lastline->next = malloc (sizeof(struct line));
  284.     }
  285.     lastline->next = NULL;
  286.     lastline->data = malloc(strlen(buf)+1);
  287.     
  288.     strcpy (lastline->data, buf);
  289.     if (isalpha(*lastline->data)) {
  290.       if (!strcmp(lastline->data, "Graphics3D"))
  291.     lastline->token = G3;
  292.       else if (!strcmp(lastline->data, "SurfaceGraphics"))
  293.     lastline->token = SG;
  294.       else if (!strcmp(lastline->data, "MeshGraphics"))
  295.     lastline->token = MG;
  296.       else if (!strcmp(lastline->data, "BezierPatch")||
  297.           !strcmp(lastline->data, "BezierGraphics`BezierPatch"))
  298.     lastline->token = BG;
  299.       else if (!strcmp(lastline->data, "Polygon"))
  300.     lastline->token = POLYGON;
  301.       else if (!strcmp(lastline->data, "Point"))
  302.     lastline->token = LINE; /* degenerate vector */
  303.       else if (!strcmp(lastline->data, "Line"))
  304.     lastline->token = LINE;
  305.       else if (!strcmp(lastline->data, "RGBColor"))
  306.     lastline->token = COLOR;
  307.       else if (!strcmp(lastline->data, "MeshRange"))
  308.     lastline->token = MESHRANGE;
  309.       else if (!strcmp(lastline->data, "Dimensions"))
  310.     lastline->token = DIMENSIONS;
  311.       else if (!strcmp(lastline->data, "Complex")) {
  312.     lastline->token = IGNORE;
  313.     complex = 1;
  314.     toss = 1;
  315.       } else {
  316.     lastline->token = IGNORE;
  317.       }
  318.     } else if (isdigit(*lastline->data) || '-'==*lastline->data) {
  319.       lastline->token = NUMBER;
  320.       /* grab real part, toss imaginary part */
  321.       if (complex) {
  322.     if (complex == 1) {
  323.       complex++;
  324.     } else if (complex == 2) {
  325.       lastline->token = IGNORE;
  326.       complex = 0;
  327.       toss = 1;
  328.     }
  329.       }
  330.     }
  331.   }
  332.   
  333.   /* we might have multiple graphics objects */
  334.   printf ("{ LIST\n\n");    
  335.   
  336.   ok = 1;
  337.   globline = lines;
  338.   for (globline = lines;globline;) {
  339.     switch (globline->token) {
  340.     case SG: /* SurfaceGraphics */
  341.       globline=globline->next;
  342.       if (globline->token == DIMENSIONS) {
  343.     globline=globline->next;
  344.     for (i = 0; i < 2 && ok; i++, globline = globline->next)
  345.       if (globline->token == NUMBER) 
  346.         size[i] = atoi(globline->data);
  347.       else ok = 0;
  348.       } else 
  349.     ok = 0;
  350.       if (!ok) {
  351.     fprintf(stderr, "can't read mesh dimensions!\n");
  352.     return;
  353.       }
  354.       if (globline->token == MESHRANGE) {
  355.     globline=globline->next;
  356.     for (i = 0; i < 4 && ok; i++, globline = globline->next) {
  357.       if (globline->token == NUMBER) 
  358.         range[i] = atof(globline->data);
  359.       else ok = 0;
  360.     }
  361.       } else ok = 0;
  362.       if (!ok) {
  363.     fprintf(stderr, "can't read mesh range!\n");
  364.     return;
  365.       }
  366.       
  367.       /* 
  368.     in SurfaceGraphics object, first we get all the colors,
  369.     then all the points: they're not interleaved as in 
  370.     the Graphics3D object. so we need to store them.
  371.     
  372.     points array is size[0] x size[1], 
  373.     color array is size[0]-1 x size[1]-1 (per face not per vertex)
  374.     so repeat the face color for the extra vertices.
  375.  
  376.     */
  377.       
  378.       /* if there are colors, store them off */
  379.       if (globline->token == COLOR) {
  380.     numcols = (size[0]-1) * (size[1]-1);
  381.     colors = malloc((size[0]-1)*sizeof(Color*));
  382.     for (i = 0; i < size[0]-1;i++)
  383.       colors[i] = malloc((size[1]-1)*sizeof(Color));
  384.     ok = 1;
  385.     for (i = 0; i < size[0]-1; i++) {
  386.       for (j=0; j < size[1]-1; j++) {
  387.         while (globline->token == COLOR && ok)
  388.           for (q=0; q < 3 && ok; q++) {
  389.         globline = globline->next;         
  390.         if (globline->token != NUMBER) {
  391.           ok = 0;
  392.           break;
  393.         } else {
  394.           colors[i][j][q]=atof(globline->data);
  395.         }
  396.           }
  397.         globline=globline->next;
  398.       }
  399.     }
  400.     if (!ok) {
  401.       fprintf(stderr, "can't read mesh color array!\n");
  402.       return;
  403.     }
  404.       }
  405.       /* Each number we get is just the z coordinate. 
  406.      Figure out x and y values based on meshrange and size.
  407.      We don't use a ZMESH because the grid isn't necessarily 
  408.      an array starting at 0,0: it depends on meshrange
  409.        */     
  410.       xincr = (range[1] - range[0]) / (size[0]-1);
  411.       yincr = (range[3] - range[2]) / (size[1]-1);
  412.       
  413.       if (numcols)
  414.     printf("\n\n{ CMESH \n");
  415.       else 
  416.     printf("\n\n{ MESH \n");
  417.       printf("\n%d %d\n",size[0], size[1]);
  418.       ok = 1;
  419.       for (i = 0; i < size[0]; i++){
  420.     for (j = 0; j < size[1]; j++) {
  421.       if (globline->token!=NUMBER) {
  422.         ok = 0; break;
  423.       }
  424.       printf(" %f ", range[0] + xincr*j); /* x */
  425.       printf(" %f ", range[2] + yincr*i); /* y */
  426.       printf(" %s ", globline->data);      /* z */
  427.       /* MESH vertices need RGBA colors 
  428.          since OOGL MESHes have per-vertex colors, repeat some.
  429.          repeat last i index, first j index.
  430.        */
  431.       if (numcols) {
  432.         printf(" %f %f %f 1\n", colors[i<size[0]-1?i:i-1][j?j-1:0][0],
  433.            colors[i<size[0]-1?i:i-1][j?j-1:0][1],
  434.            colors[i<size[0]-1?i:i-1][j?j-1:0][2]);
  435.       } else printf("\n");
  436.       globline=globline->next;
  437.     }
  438.       }
  439.       if (!ok) {
  440.     fprintf(stderr, "can't read mesh points array!\n");
  441.     return;
  442.       } else 
  443.     printf("} #end of MESH\n"); 
  444.       break;
  445.     case MG:  /* MeshGraphics */
  446.       globline=globline->next;
  447.       if (globline->token == DIMENSIONS) {
  448.     globline=globline->next;
  449.     for (i = 0; i < 2 && ok; i++, globline = globline->next)
  450.       if (globline->token == NUMBER) 
  451.         size[i] = atoi(globline->data);
  452.       else ok = 0;
  453.       } else 
  454.     ok = 0;
  455.       if (!ok) {
  456.     fprintf(stderr, "can't read mesh dimensions!\n");
  457.     return;
  458.       }
  459.       
  460.       /* 
  461.     in MeshGraphics object, first we get all the colors,
  462.     then all the points: they're not interleaved as in 
  463.     the Graphics3D object. so we need to store them.
  464.     
  465.     points array is size[0] x size[1], 
  466.     color array is size[0]-1 x size[1]-1 (per face not per vertex)
  467.     so repeat the face color for the extra vertices.
  468.  
  469.     */
  470.       
  471.       /* if there are colors, store them off */
  472.       if (globline->token == COLOR) {
  473.     numcols = (size[0]-1) * (size[1]-1);
  474.     colors = malloc((size[0]-1)*sizeof(Color*));
  475.     for (i = 0; i < size[0]-1;i++)
  476.       colors[i] = malloc((size[1]-1)*sizeof(Color));
  477.     ok = 1;
  478.     for (i = 0; i < size[0]-1; i++) {
  479.       for (j=0; j < size[1]-1; j++) {
  480.         while (globline->token == COLOR && ok)
  481.           for (q=0; q < 3 && ok; q++) {
  482.         globline = globline->next;         
  483.         if (globline->token != NUMBER) {
  484.           ok = 0;
  485.           break;
  486.         } else {
  487.           colors[i][j][q]=atof(globline->data);
  488.         }
  489.           }
  490.         globline=globline->next;
  491.       }
  492.     }
  493.     if (!ok) {
  494.       fprintf(stderr, "can't read mesh color array!\n");
  495.       return;
  496.     }
  497.       }
  498.       /* Each number we get is just the x,y or z coordinate. 
  499.      We don't use a ZMESH because the grid isn't necessarily 
  500.      an array starting at 0,0: it depends on meshrange
  501.        */     
  502.       
  503.       if (numcols)
  504.     printf("\n\n{ CMESH \n");
  505.       else 
  506.     printf("\n\n{ MESH \n");
  507.       printf("\n%d %d\n",size[0], size[1]);
  508.       ok = 1;
  509.       for (i = 0; i < size[0]; i++){
  510.     for (j = 0; j < size[1]; j++) {
  511.       if (globline->token!=NUMBER) {
  512.         ok = 0; break;
  513.       }
  514.       printf(" %s ", globline->data);      /* x */
  515.           globline=globline->next;
  516.       if (globline->token!=NUMBER) {
  517.         ok = 0; break;
  518.       }
  519.       printf(" %s ", globline->data);      /* y */
  520.           globline=globline->next;
  521.       if (globline->token!=NUMBER) {
  522.         ok = 0; break;
  523.       }
  524.       printf(" %s ", globline->data);      /* z */
  525.       /* MESH vertices need RGBA colors 
  526.          since OOGL MESHes have per-vertex colors, repeat some.
  527.          repeat last i index, first j index.
  528.        */
  529.       if (numcols) {
  530.         printf(" %f %f %f 1\n", colors[i<size[0]-1?i:i-1][j?j-1:0][0],
  531.            colors[i<size[0]-1?i:i-1][j?j-1:0][1],
  532.            colors[i<size[0]-1?i:i-1][j?j-1:0][2]);
  533.       } else printf("\n");
  534.       globline=globline->next;
  535.     }
  536.       }
  537.       if (!ok) {
  538.     fprintf(stderr, "can't read mesh points array!\n");
  539.     return;
  540.       } else 
  541.     printf("} #end of MESH\n"); 
  542.     break;
  543.     case BG:  /* BezierGraphics */
  544.       globline=globline->next;
  545.       if (globline->token == DIMENSIONS) {
  546.     globline=globline->next;
  547.     for (i = 0; i < 2 && ok; i++, globline = globline->next)
  548.       if (globline->token == NUMBER) 
  549.         size[i] = atoi(globline->data);
  550.       else ok = 0;
  551.       } else 
  552.     ok = 0;
  553.       if (!ok) {
  554.     fprintf(stderr, "can't read Bezier patch dimensions!\n");
  555.     return;
  556.       }
  557.       
  558.       /* 
  559.     in BezierGraphics object, first we get all the colors,
  560.     then all the points.
  561.     
  562.     points array is size[0] x size[1], 
  563.     color array is 2x2.
  564.  
  565.     */
  566.       
  567.       /* if there are colors, store them off */
  568.       if (globline->token == COLOR) {
  569.     numcols = 2*2;
  570.     colors = malloc(2*sizeof(Color*));
  571.     for (i = 0; i < 2;i++)
  572.       colors[i] = malloc(2*sizeof(Color));
  573.     ok = 1;
  574.     for (i = 0; i < 2; i++) {
  575.       for (j=0; j < 2; j++) {
  576.         while (globline->token == COLOR && ok)
  577.           for (q=0; q < 3 && ok; q++) {
  578.         globline = globline->next;         
  579.         if (globline->token != NUMBER) {
  580.           ok = 0;
  581.           break;
  582.         } else {
  583.           colors[i][j][q]=atof(globline->data);
  584.         }
  585.           }
  586.         globline=globline->next;
  587.       }
  588.     }
  589.     if (!ok) {
  590.       fprintf(stderr, "can't read mesh color array!\n");
  591.       return;
  592.     }
  593.       }
  594.       /* Each number we get is just the x,y or z coordinate.  */
  595.       
  596.       if (numcols)
  597.     printf("\n\n{ CBEZ");
  598.       else 
  599.     printf("\n\n{ BEZ");
  600.       printf("%d%d3\n",size[1]-1, size[0]-1); /* reverse order */
  601.       ok = 1;
  602.       for (i = 0; i < size[0]; i++){
  603.     for (j = 0; j < size[1]; j++) {
  604.       if (globline->token!=NUMBER) {
  605.         ok = 0; break;
  606.       }
  607.       printf(" %s ", globline->data);      /* x */
  608.           globline=globline->next;
  609.       if (globline->token!=NUMBER) {
  610.         ok = 0; break;
  611.       }
  612.       printf(" %s ", globline->data);      /* y */
  613.           globline=globline->next;
  614.       if (globline->token!=NUMBER) {
  615.         ok = 0; break;
  616.       }
  617.       printf(" %s ", globline->data);      /* z */
  618.       /* print colors */
  619.       if (numcols) {
  620.         printf(" %f %f %f 1\n", colors[i][j][0], colors[i][j][1],
  621.            colors[i][j][2]);
  622.       } else printf("\n");
  623.       globline=globline->next;
  624.     }
  625.       }
  626.       if (!ok) {
  627.     fprintf(stderr, "can't read control points array!\n");
  628.     return;
  629.       } else 
  630.     printf("} #end of BEZ\n"); 
  631.     break;
  632.     case G3: /* Graphics3D */
  633.       globline=globline->next;
  634.       npolypoints=npolys=npolycolors=0;
  635.       nvectpoints=nvects=nvectcolors=0;
  636.       numnums=numcols=0;
  637.       state = IGNORE;
  638.       for (curline=globline;;curline=curline->next) {
  639.     if (!curline || (curline->token != NUMBER)) {
  640.       
  641.       if (state == LINE) {
  642.         nvects++;
  643.         nvectpoints += numnums/3;
  644.         if (numcols)
  645.           nvectcolors++;
  646.         numcols = 0;
  647.       } else if (state == POLYGON) {
  648.         npolys++;
  649.         npolypoints += numnums/3;
  650.         if (numcols)
  651.           npolycolors++;
  652.       }
  653.       if (!curline || ismajor(curline->token))
  654.         break; /* go back to main loop */
  655.       
  656.       if (curline->token == COLOR)
  657.         numcols++;
  658.       
  659.       state=curline->token;
  660.       numnums=0;
  661.     } else            /* it's a number */
  662.       numnums++;
  663.       }      
  664.       if (npolys) {
  665.     
  666.     printf ("\n{ = OFF\n");
  667.     printf ("%d %d %d\n",npolypoints, npolys, 0);
  668.     
  669.     { /* vertex list */
  670.       struct line *curline;
  671.       enum st state=IGNORE;
  672.       char coordnum=0;
  673.  
  674.       for (curline=globline; curline && !ismajor(curline->token);
  675.            curline=curline->next) {
  676.         if (curline->token != NUMBER) {
  677.           state = curline->token;
  678.           coordnum=0;
  679.           printf("\n");
  680.         } else
  681.           if (state == POLYGON) {
  682.         printf("%s",curline->data);
  683.         if(coordnum++%3 == 2)
  684.           printf("\n");
  685.         else
  686.           printf(" ");
  687.           }
  688.       }
  689.     }
  690.     { /* face list (maybe includes color) */
  691.       Color c;
  692.       char hascolor = 0;    /* so far. */
  693.       char thiscolor=1;    /* if this polygon has had its color
  694.                  * tacked on. */
  695.       char cnum=3;        /* 3 == not doing color now. */
  696.       struct line *curline;
  697.       enum st state=IGNORE;
  698.       int pointnum=0;
  699.       
  700.       c[0] = c[1] = c[2] = 0;
  701.       for (curline=globline;;curline=curline->next) {
  702.         if (!thiscolor && hascolor &&
  703.         (!curline || curline->token != NUMBER)) {
  704.           /* OFF faces need just RGB colors, no alpha! */
  705.           printf(" %f %f %f",c[0],c[1],c[2]);
  706.           thiscolor = 1;
  707.         }
  708.         if (!curline || ismajor(curline->token))
  709.           break; /* back to main loop */
  710.         switch (curline->token) {
  711.         case COLOR:
  712.           cnum = 0;
  713.           state = COLOR;
  714.           hascolor = 1;
  715.           break;
  716.         case POLYGON:
  717.           {
  718.         struct line *countline;
  719.         int numvert=0;
  720.         
  721.         thiscolor = !hascolor;
  722.         
  723.         cnum = 3;
  724.         state = POLYGON;
  725.         for (countline = curline->next; countline; countline=countline->next) {
  726.           if (countline->token != NUMBER)
  727.             break;
  728.           numvert++;
  729.         }
  730.         
  731.         printf("\n%d",numvert/3);
  732.           }
  733.           break;
  734.         case NUMBER:
  735.           if (state == POLYGON) {
  736.         if (pointnum++%3 == 2)
  737.           printf(" %d",pointnum/3-1);
  738.           } else if (state==COLOR) {
  739.         if (cnum<3)
  740.           c[(int)cnum++] = atof(curline->data);
  741.           }
  742.           break;
  743.         default:
  744.           state = curline->token;
  745.           break;
  746.         }
  747.       }
  748.     }
  749.     printf("\n\n} #end of OFF\n");
  750.       }
  751.       if (nvects) {
  752.     printf("\n{ = VECT\n");
  753.     printf("%d %d %d\n",nvects, nvectpoints, nvectcolors);
  754.     {
  755.       struct line *curline;
  756.       enum st state=IGNORE;
  757.       int numvert=0, numcol=0;
  758.       for (curline=globline;;curline=curline->next) {
  759.         if (numvert && (!curline || curline->token != NUMBER)) {
  760.           printf("%d ",numvert/3);
  761.           numvert = 0;
  762.         }
  763.         if (!curline || ismajor(curline->token))
  764.           break; /* back to main loop */
  765.         if(curline->token == NUMBER && state==LINE)
  766.           numvert++;
  767.         else
  768.           state=curline->token;
  769.       }
  770.       printf("\n\n");
  771.       for (curline=globline; curline && !ismajor(curline->token);
  772.            curline=curline->next) {
  773.         if (curline->token == LINE) {
  774.           printf("%d ",numcol?1:0);
  775.           numcol = 0;
  776.         }
  777.         if(curline->token == COLOR)
  778.           numcol++;
  779.       }
  780.       printf("\n\n");
  781.     }
  782.     {
  783.       struct line *curline;
  784.       enum st state=IGNORE;
  785.       int coordno=0;
  786.       for (curline=globline; curline && !ismajor(curline->token);
  787.            curline=curline->next) {
  788.         if (curline->token != NUMBER)
  789.           state = curline->token;
  790.         else
  791.           if (state == LINE) {
  792.         printf("%s%c",curline->data,((++coordno)%3)?' ':'\n');
  793.           }
  794.       }
  795.     }
  796.     {
  797.       struct line *curline;
  798.       Color c;
  799.       char hascolor=0;
  800.       char cnum=3;
  801.       
  802.       c[0] = c[1] = c[2] = 0;
  803.       
  804.       for (curline=globline; curline && !ismajor(curline->token);
  805.            curline=curline->next) {
  806.         if (curline->token == NUMBER) {
  807.           if (cnum<3) {
  808.         c[(int)cnum++]=atof(curline->data);
  809.         hascolor = 1;
  810.           }
  811.         } else if (curline->token == LINE) {
  812.           /* note VECTs need RGBA colors */
  813.           if(hascolor)
  814.         printf("%f %f %f 1\n",c[0],c[1],c[2]);
  815.           hascolor = 0;
  816.         } else if (curline->token == COLOR)
  817.           cnum = 0;
  818.         
  819.       }
  820.     }
  821.     printf("\n} #end of VECT\n");
  822.       }
  823.       globline = curline;
  824.     break;
  825.     default: fprintf(stderr, "math2oogl: unexpected data: %s\n", globline->data); return;
  826.     } /* end switch (globline->token) */
  827.  }
  828.   printf ("\n} #end of LIST\n");
  829.   if (togv) {        /* end our (geometry */
  830.     printf("\n)\n");
  831.   }
  832.   return 0;
  833. }
  834.